<template>
  <div class="survey-container">
    <!-- 调查卡片 -->
    <div class="survey-card">
      <!-- 标题区域 -->
      <div class="survey-header">
        <h1 class="survey-title">京东首页-用户体验调查</h1>
        <div class="survey-description">
          <p>尊敬的用户：</p>
          <p>您好！为了给您提供更好的产品和服务，我们希望收集您使用<span>京东首页</span>时的看法或建议。对您的配合和支持表示衷心感谢！</p>
        </div>
      </div>

      <!-- 表单区域 -->
      <form class="survey-form" @submit.prevent="handleSubmit">
        <!-- 问题1：反馈输入 -->
        <div class="form-group">
          <label class="form-label">
            <span class="required">*</span>
            1. 如果您在使用京东首页时，有什么好或不好的地方，请在此留言！我们会关注您的反馈，不断优化产品，为您提供更好的服务！
          </label>
          <textarea v-model="feedback" class="form-textarea" :class="{ 'error': submitFailed && !feedback }"
            required></textarea>
          <p v-if="submitFailed && !feedback" class="error-message">请输入您的反馈意见</p>
        </div>

        <!-- 问题2：图片上传 -->
        <div class="form-group">
          <label class="form-label">
            2. 为了便于我们核查问题，可上传遇到问题的截图。
          </label>
          <div class="upload-area">
            <label for="file-upload" class="upload-btn">
              <i class="upload-icon">+</i>
              <span class="upload-text">上传图片</span>
            </label>
            <input id="file-upload" type="file" accept="image/*" @change="handleFileUpload" class="file-input">
            <span class="upload-hint">限制大小20M</span>
          </div>

          <!-- 预览已上传的图片 -->
          <div v-if="selectedFile" class="file-preview">
            <img :src="filePreviewUrl" alt="预览图" class="preview-image">
            <button type="button" class="remove-file" @click="removeFile">
              <i class="remove-icon">×</i>
            </button>
          </div>
        </div>

        <!-- 问题3：手机号码 -->
        <div class="form-group">
          <label class="form-label">
            3. 为了方便我们与您联系，可填写您的手机号。
            <span class="optional">(选填，信息仅作为内部资料绝不外泄)</span>
          </label>
          <input v-model="phone" type="tel" class="form-input"
            :class="{ 'error': submitFailed && phone && !isValidPhone }" @input="validatePhone">
          <p v-if="submitFailed && phone && !isValidPhone" class="error-message">请输入有效的手机号码</p>
        </div>

        <!-- 提交按钮 -->
        <div class="submit-section">
          <button type="submit" class="submit-btn">提交</button>
        </div>
      </form>
    </div>

    <!-- 提交成功弹窗 -->
    <div v-if="showSuccessModal" class="modal-overlay">
      <div class="modal-content">
        <div class="success-icon">✓</div>
        <h3 class="modal-title">提交成功</h3>
        <p class="modal-message">感谢您的反馈，我们会认真对待您的建议！</p>
        <button class="modal-btn" @click="showSuccessModal = false">确定</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 表单数据
const feedback = ref('');
const phone = ref('');
const selectedFile = ref(null);
const filePreviewUrl = ref('');
const submitFailed = ref(false);
const showSuccessModal = ref(false);
const isValidPhone = ref(true);

// 处理文件上传
const handleFileUpload = (e) => {
  const file = e.target.files[0];
  if (file) {
    // 检查文件大小
    if (file.size > 20 * 1024 * 1024) {
      alert('文件大小不能超过20M');
      return;
    }

    selectedFile.value = file;
    // 创建预览URL
    filePreviewUrl.value = URL.createObjectURL(file);
  }
};

// 移除已上传的文件
const removeFile = () => {
  URL.revokeObjectURL(filePreviewUrl.value);
  filePreviewUrl.value = '';
  selectedFile.value = null;
  // 重置文件输入，允许重新选择相同文件
  document.getElementById('file-upload').value = '';
};

// 验证手机号
const validatePhone = () => {
  const phoneRegex = /^1[3-9]\d{9}$/;
  isValidPhone.value = phone.value ? phoneRegex.test(phone.value) : true;
};

// 处理表单提交
const handleSubmit = () => {
  // 基本验证
  if (!feedback.value || !feedback.value.trim()) {
    submitFailed.value = true;
    return;
  }

  // 验证手机号（如果填写）
  if (phone.value && !isValidPhone.value) {
    submitFailed.value = true;
    return;
  }

  // 模拟提交
  console.log({
    feedback: feedback.value,
    phone: phone.value,
    file: selectedFile.value ? selectedFile.value.name : null
  });

  // 显示成功弹窗
  showSuccessModal.value = true;

  // 重置表单
  feedback.value = '';
  phone.value = '';
  removeFile();
  submitFailed.value = false;
};
</script>

<style scoped lang="scss">
// 基础样式变量
$primary-color: #165DFF;
$primary-dark: #0E42D2;
$text-color: #333333;
$text-secondary: #666666;
$text-light: #999999;
$border-color: #DDDDDD;
$error-color: #F53F3F;
$bg-color: #F2F3F5;
$card-bg: #FFFFFF;
$shadow: 0 4px 12px rgba(0, 0, 0, 0.05);

.survey-container {
  min-height: 100vh;
  padding: 40px 70px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  box-sizing: border-box;
}

.survey-card {
  width: 100%;
  max-width: 1200px;
  background-color: $card-bg;
  border-radius: 8px;
  box-shadow: $shadow;
  padding: 30px 40px;
  box-sizing: border-box;
}

.survey-header {
  margin-bottom: 30px;

  .survey-title {
    font-size: 22px;
    color: $text-color;
    text-align: center;
    margin-bottom: 20px;
    font-weight: 600;
  }

  .survey-description {
    color: $text-secondary;
    line-height: 1.6;
    font-size: 14px;

    p {
      margin-bottom: 8px;

      span {
        color: rgb(250, 100, 0);
        font-weight: 700;
      }

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

.survey-form {
  width: 100%;
}

.form-group {
  margin-bottom: 25px;

  &:last-of-type {
    margin-bottom: 35px;
  }
}

.form-label {
  display: block;
  color: $text-color;
  font-size: 15px;
  margin-bottom: 10px;
  line-height: 1.5;

  .required {
    color: $error-color;
    margin-right: 4px;
  }

  .optional {
    color: $text-light;
    font-size: 13px;
    margin-left: 5px;
  }
}

.form-textarea {
  width: 100%;
  min-height: 120px;
  padding: 12px 15px;
  border: 1px solid $border-color;
  border-radius: 4px;
  font-size: 14px;
  resize: vertical;
  box-sizing: border-box;
  transition: border-color 0.2s;

  &:focus {
    outline: none;
    border-color: $primary-color;
  }

  &::placeholder {
    color: $text-light;
  }

  &.error {
    border-color: $error-color;
  }
}

.form-input {
  width: 100%;
  height: 44px;
  padding: 0 15px;
  border: 1px solid $border-color;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
  transition: border-color 0.2s;

  &:focus {
    outline: none;
    border-color: $primary-color;
  }

  &::placeholder {
    color: $text-light;
  }

  &.error {
    border-color: $error-color;
  }
}

.upload-area {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 15px;
}

.upload-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 109px;
  height: 31px;
  border-radius: 16px;
  font-size: 14px;
  cursor: pointer;
  padding: 5px 12px;
  color: #2c61ff;
  border: 1px solid #2c61ff;

  &:hover {
    border-color: $primary-color;
    background-color: rgba(22, 93, 255, 0.03);
  }

  .upload-icon {
    font-size: 18px;
    margin-right: 6px;
  }
}

.file-input {
  display: none;
}

.upload-hint {
  color: $text-light;
  font-size: 13px;
}

.file-preview {
  position: relative;
  display: inline-block;
  margin-top: 10px;

  .preview-image {
    width: 120px;
    height: 120px;
    object-fit: cover;
    border-radius: 4px;
    border: 1px solid $border-color;
  }

  .remove-file {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background-color: $error-color;
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    font-size: 16px;
    line-height: 1;

    &:hover {
      background-color: #E02020;
    }
  }
}

.error-message {
  color: $error-color;
  font-size: 13px;
  margin-top: 5px;
  line-height: 1;
}

.submit-section {
  text-align: center;
}

.submit-btn {
  width: 160px;
  height: 48px;
  background-color: $primary-color;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;

  &:hover {
    background-color: $primary-dark;
  }

  &:active {
    background-color: #0A34A5;
  }
}

// 弹窗样式
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  width: 100%;
  max-width: 360px;
  background-color: white;
  border-radius: 8px;
  padding: 30px 20px;
  text-align: center;

  .success-icon {
    width: 60px;
    height: 60px;
    margin: 0 auto 20px;
    background-color: #00B42A;
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    font-weight: bold;
  }

  .modal-title {
    font-size: 18px;
    color: $text-color;
    margin-bottom: 10px;
    font-weight: 600;
  }

  .modal-message {
    color: $text-secondary;
    font-size: 14px;
    margin-bottom: 25px;
    line-height: 1.5;
  }

  .modal-btn {
    width: 100%;
    max-width: 160px;
    height: 44px;
    background-color: $primary-color;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.2s;

    &:hover {
      background-color: $primary-dark;
    }
  }
}

// // 响应式调整
// @media (max-width: 768px) {
//   .survey-card {
//     padding: 20px 15px;
//   }

//   .survey-title {
//     font-size: 20px;
//   }

//   .form-group {
//     margin-bottom: 20px;
//   }

//   .upload-area {
//     flex-wrap: wrap;
//   }
// }
</style>
